package com.nq.controller.agent;


import com.nq.common.ServerResponse;

import com.nq.dao.MyDao;
import com.nq.service.IUserPositionService;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;


@Controller
@RequestMapping({"/agent/position/"})

public class AgentPositionController {
    private static final Logger log = LoggerFactory.getLogger(AgentPositionController.class);


    @Autowired
    IUserPositionService iUserPositionService;

    //分页查询持仓管理 所有融资平仓单信息及模糊查询
    @RequestMapping({"list.do"})
    @ResponseBody
    public ServerResponse list(HttpServletRequest request, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                               @RequestParam(value = "positionType", required = false) Integer positionType,
                               @RequestParam(value = "state", required = false) Integer state,
                               @RequestParam(value = "userId", required = false) Integer userId,
                               @RequestParam(value = "agentId", required = false) Integer agentId,
                               @RequestParam(value = "stockCode", required = false) String stockCode,
                               @RequestParam(value = "positionSn", required = false) String positionSn,
                               @RequestParam(value = "userPhone", required = false) String userPhone,
                               @RequestParam(value = "beginTime", required = false) String beginTime,
                               @RequestParam(value = "endTime", required = false) String endTime) {
        return this.iUserPositionService.listByAgent(stockCode, positionType, state, userId, agentId, positionSn, userPhone, beginTime, endTime, request, pageNum, pageSize);
    }


    @RequestMapping({"update.do"})
    @ResponseBody
    public Object update(@RequestParam Map<String, Object> position) {
        boolean flag = MyDao.updateById("user_position", position);
        if (flag) {
            return ServerResponse.createBySuccess("操作成功");
        }
        return ServerResponse.createByErrorMsg("操作失败");
    }

    //持仓管理 强制平仓操作
    @RequestMapping({"sell.do"})
    @ResponseBody
    public ServerResponse sell(String positionSn) throws Exception {
        ServerResponse serverResponse = null;
        try {
            serverResponse = this.iUserPositionService.sell(positionSn, 0);
        } catch (Exception e) {
            log.error("Forced liquidation abnormal information = {}", e);
        }
        return serverResponse;
    }

    //锁仓/解仓操作
    @RequestMapping({"lock.do"})
    @ResponseBody
    public ServerResponse lock(@RequestParam("positionId") Integer positionId, @RequestParam("state") Integer state, @RequestParam(value = "lockMsg", required = false) String lockMsg) {
        return this.iUserPositionService.lock(positionId, state, lockMsg);
    }

    @RequestMapping({"del.do"})
    @ResponseBody
    public ServerResponse del(@RequestParam("positionId") Integer positionId) {
        return this.iUserPositionService.del(positionId);
    }

}

